package com.digitalchina.lxnsy.update;

import android.app.Activity;
import android.util.Log;

import com.afollestad.materialdialogs.MaterialDialog;

import org.lzh.framework.updatepluginlib.callback.UpdateDownloadCB;
import org.lzh.framework.updatepluginlib.creator.DownloadCreator;
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.SafeDialogOper;

import java.io.File;

public class CustomNeedDownloadCreator implements DownloadCreator {
    @Override
    public UpdateDownloadCB create(Update update, Activity activity) {
        if (activity == null || activity.isFinishing()) {
            Log.e("DownDialogCreator--->","show download dialog failed:activity was recycled or finished");
            return null;
        }

        final MaterialDialog dialog = new MaterialDialog.Builder(activity)
                .progress(false, 100, true)
                .cancelable(false)
                .canceledOnTouchOutside(false)
                .build();

        SafeDialogOper.safeShowDialog(dialog);
        return new UpdateDownloadCB() {
            @Override
            public void  onUpdateStart() {
            }

            @Override
            public void onUpdateComplete(File file) {
                SafeDialogOper.safeDismissDialog(dialog);
            }

            @Override
            public void onUpdateProgress(long current, long total) {
                int percent = (int) (current * 1.0f / total * 100);
                dialog.setProgress(percent);
            }

            @Override
            public void onUpdateError(Throwable t) {
                SafeDialogOper.safeDismissDialog(dialog);
            }
        };
    }
}
